from . import BaseListItem
from ..log import log_gui
from .. import list
from .. import screen
from ..app import GuiApp
from ..background import VerticalGradientBackground
from ..background import SolidBackground
from icon import Icon
from ..rect import Rect
import gtk
import gobject
import pango

def EscapeHtml(str):
  return str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

class TextItem(list.BaseListItem):
  submenu_icons={
    BaseListItem.STATE_NORMAL: Icon('gui/assets/submenu.png', 10),
    BaseListItem.STATE_HIGHLIGHTED: Icon('gui/assets/submenu.png', 10),
  }

  new_icons={
    BaseListItem.STATE_NORMAL: Icon('gui/assets/new.png', 10),
    BaseListItem.STATE_HIGHLIGHTED: Icon('gui/assets/new.png', 10),
  }

  HEIGHT = 75
  FONT_SIZE = 25

  def __init__(self, height, font_size, label, right_icons=None,
      left_icons=None):
    list.BaseListItem.__init__(self, height)
    self.font_size = font_size
    self.label = unicode(label)
    self.left_margin = 10
    self.right_icons = right_icons
    self.left_icons = left_icons

  def __cmp__(self, other):
    return cmp(self.label, other.label)

  def Draw(self, list_widget, screen, screen_window, x, y, dest_pixmap):
    state = self.GetState()
    if state not in self.caches:
      #log_gui("Create cache")
      cache = self.CreateCache(state, list_widget, screen, screen_window)
    else:
      cache = self.caches[state]

    self.CopyCacheToWindow(cache, dest_pixmap, x, y, screen.rect.width,
        self.height, screen)

  def CopyCacheToWindow(self, cache, dest_pixmap, x, y, width, height,
      screen):
    gc = screen.GC(dest_pixmap)
    dest_pixmap.draw_drawable(gc, cache, 0, 0, int(x), int(y), width, height)

  def CreateCache(self, state, list_widget, screen, screen_window):
    cache = gtk.gdk.Pixmap(screen_window, screen.rect.width,
        self.height, -1)
    self.caches[state] = cache

    if state == list.BaseListItem.STATE_NORMAL:
      background = SolidBackground("#ffffff")
      fgcolor = "#000000"
    else:
      background = VerticalGradientBackground("#404ee2", "#40a0ff")
      fgcolor = "#ffffff"
    background.DrawToBuffer(cache, screen_window,
        Rect((0, 0, list_widget.rect.width, self.height)))

    gc = screen_window.new_gc()
    context = screen.app().create_pango_context()
    try:
      if self.highlighted:
        mytext = pango.parse_markup('<span foreground="' + fgcolor + '">' +
            EscapeHtml(self.label) + '</span>', u'\x00')
      else:
        mytext = pango.parse_markup('<span foreground="' + fgcolor + '">' +
            EscapeHtml(self.label) + '</span>', u'\x00')
    except gobject.GError:
      mytext = pango.parse_markup('<span foreground="' + self.fontcolor +
          '">-error-</span>', u'\x00')
    layout = screen.app().create_pango_layout(mytext[1])
    layout.set_attributes(mytext[0])
    desc = pango.FontDescription('Helvetica %d' % self.font_size)
    layout.set_font_description(desc)
    lx, ly = layout.get_pixel_size()
    if self.align_horizontal == list.BaseListItem.ALIGN_LEFT:
      x = self.left_margin
    else:
      x = (list_widget.rect.width / 2) - (lx / 2)
    y = (self.height - ly) / 2
    #log_gui("Draw layout with gc",gc, "x=",x,"y=", y, "layout=",layout)

    clip_x = 0
    clip_width = list_widget.rect.width - x

    if self.left_icons and state in self.left_icons:
      icon = self.left_icons[state]
      icon.Draw(
          cache,
          0,
          int((self.height - icon.GetHeight()) / 2),
          None)
      x += icon.GetWidth()
      clip_x += icon.GetWidth()
      clip_width -= icon.GetWidth()

    if self.right_icons and state in self.right_icons:
      icon = self.right_icons[state]
      icon.Draw(
          cache,
          list_widget.rect.width - icon.GetWidth() - icon.GetHorizontalMargin(),
          int((self.height - icon.GetHeight()) / 2),
          None)
      clip_width -= icon.GetWidth() + icon.GetHorizontalMargin()

    gc.set_clip_rectangle(
        Rect((clip_x, 0, clip_width, self.height)).GetGdkRectangle())

    cache.draw_layout(gc,
        x,
        y,
        layout)

    return cache

class TextItemToSubmenu(TextItem):
  def __init__(self, app, label, submenu_name):
    TextItem.__init__(self, TextItem.HEIGHT, TextItem.FONT_SIZE, label,
        right_icons=TextItem.submenu_icons, left_icons=None)
    self.OnTap = self.LoadMenuOnTap(app, submenu_name, GuiApp.ENTER_FROM_RIGHT)

class BasicTextItem(TextItem):
  def __init__(self, app, label, callback, is_submenu=False, is_new=False):
    if is_submenu:
      right_icons = TextItem.submenu_icons
    else:
      right_icons = None
    if is_new:
      left_icons = TextItem.new_icons
    else:
      left_icons = None
    TextItem.__init__(self, TextItem.HEIGHT, TextItem.FONT_SIZE, label,
        right_icons=right_icons, left_icons=left_icons)
    self.callback = callback

  def OnTap(self):
    self.callback()

